Base class for all food resources contained in patches and consumed by bands. Contains a number of useful methods for harvesting and replenshing a resource. This class on its own is “abstract” and should not be instantiated.
Creates a new food resource instance with a certain carrying capacity (per/km^2) and an area in which the resource exists (km^2). This class on its own is “abstract” and should only be instantiated in subclasses.
# File lib/food_resource.rb, line 18 def initialize(cc, area) @capacity = @size = cc * area # Resources start at full capacity @carrying_capacity = cc # Chance per neighbouring healthy patch to repopulate from depletion (percent) @repopulation_chance = 10 @tend_time = 0 @name = :FoodResource @energy_harvested = 0.0 end
Returns the total amount of energy available for consumption from this resource.
# File lib/food_resource.rb, line 53 def available_energy @size * @energy_value end
If this resource has been entirely consumed then it is depleted.
# File lib/food_resource.rb, line 47 def depleted? @size <= 0.00001 end
Regrow the resource if it has been depleted. This is generally called by Scape
# File lib/food_resource.rb, line 59 def grow if @capacity - @size > 0.000001 exponent = Math.exp(@growth_rate) se = @size * exponent @size = (se * @capacity) / (@capacity - @size + se) end end
Attempt to harvest this resource for consumption. Harvesters “ask” for a certain number of resources (energy_wanted) and this method returns the number of calories that could be harvested from the resource. It also updates the quantity of the resource accordingly.
# File lib/food_resource.rb, line 32 def harvest(energy_wanted) quantity_wanted = energy_wanted / @energy_value @energy_harvested = 0 if quantity_wanted > @size @energy_harvested = available_energy @size = 0 else @energy_harvested = energy_wanted @size -= quantity_wanted end @energy_harvested end
The hash code for resources is overloaded so that it is possible to cache pre-calculated NAR values. This works by including the size of the resource in the hash code calculation: if the size changes the cache for the NAR of the resource is invalidated.
# File lib/food_resource.rb, line 96 def hash [@size, self.object_id].hash end
Returns the percentage of resource available; this is used for patch visualization.
# File lib/food_resource.rb, line 88 def percent_full @size * @energy_value end
After depletion, resources can be repopulated; i.e. reset their size to the viable size.
# File lib/food_resource.rb, line 82 def repopulate @size = @viable_size end
Logging output
# File lib/food_resource.rb, line 69 def to_s "Name: #{@name} | Size: #{@size}/#{@capacity}" end
Returns true if this resource is dense enough to be what the researchers deemed “viable”. Viable patches have a chance of causing depleted resources in neighbouring patches to be repopulated.
# File lib/food_resource.rb, line 76 def viable? @size > @viable_size end